package com.atguigu.ggkt.wechat.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.atguigu.ggkt.exception.GgktException;
import com.atguigu.ggkt.model.wechat.Menu;
import com.atguigu.ggkt.vo.wechat.MenuVo;
import com.atguigu.ggkt.wechat.mapper.MenuMapper;
import com.atguigu.ggkt.wechat.service.MenuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.SneakyThrows;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 订单明细 订单明细 服务实现类
 * </p>
 *
 * @author Jetwang
 * @since 2022-07-02
 */
@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements MenuService {
    @Autowired
    private WxMpService wxMpService;

    @Override
    public List<Menu> findMenuOneInfo() {
        List<Menu> oneMenus = query().eq("parent_id", 0).list();
        return oneMenus;
    }

    @Override
    public List<MenuVo> findMenuInfo() {
        // 1.获取所有一级菜单
        List<Menu> menuOneList = findMenuOneInfo();
        // 2.获取所有菜单，并封装到一级菜单
        List<MenuVo> menuVoList = menuOneList.stream().map(menuOne -> {
            MenuVo menuOneVo = new MenuVo();
            BeanUtils.copyProperties(menuOne, menuOneVo);
            Long oneId = menuOne.getId();
            // 2.1.二级菜单
            List<Menu> menuTwoList = query().eq("parent_id", oneId).list();
            List<MenuVo> menuTwoVoList = menuTwoList.stream().map(menuTwo -> {
                MenuVo menuTwoVo = new MenuVo();
                BeanUtils.copyProperties(menuTwo, menuTwoVo);
                return menuTwoVo;
            }).collect(Collectors.toList());
            menuOneVo.setChildren(menuTwoVoList);
            // 3.返回
            return menuOneVo;
        }).collect(Collectors.toList());
        return menuVoList;
    }

    @SneakyThrows
    @Override
    public void syncMenu() {
        List<MenuVo> menuVoList = this.findMenuInfo();
        // 菜单
        JSONArray buttonList = new JSONArray();
        for(MenuVo oneMenuVo : menuVoList) {
            JSONObject one = new JSONObject();
            one.put("name", oneMenuVo.getName());
            JSONArray subButton = new JSONArray();
            for(MenuVo twoMenuVo : oneMenuVo.getChildren()) {
                JSONObject view = new JSONObject();
                view.put("type", twoMenuVo.getType());
                if(twoMenuVo.getType().equals("view")) {
                    view.put("name", twoMenuVo.getName());
                    view.put("url", "http://h4i5kd.natappfree.cc/#"
                            +twoMenuVo.getUrl());
                } else {
                    view.put("name", twoMenuVo.getName());
                    view.put("key", twoMenuVo.getMeunKey());
                }
                subButton.add(view);
            }
            one.put("sub_button", subButton);
            buttonList.add(one);
        }
        // 菜单
        JSONObject button = new JSONObject();
        button.put("button", buttonList);
        try {
            String menuId = this.wxMpService.getMenuService().menuCreate(button.toJSONString());
            System.out.println(menuId + ":" + menuId);
        } catch (WxErrorException e) {
            e.printStackTrace();
            throw new GgktException(20001, "公众号菜单同步失败");
        }
    }

    @SneakyThrows
    @Override
    public void removeMenu() {
        wxMpService.getMenuService().menuDelete();
    }
}
